package com.ygp.basic;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Slf4j(topic = "c.testJoin3")
public class TestJoin3 {
    private static final int init = 10;

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 20; i++) {
            log.debug("--------------------------");
            List<Integer> dataList = Arrays.asList(1, 3, 5);
            List<Integer> result = new ArrayList<>();//11 13 15
            List<CompletableFuture<Integer>> futures = new ArrayList<>();
            for (Integer each : dataList) {
                CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> sum(each, result));
                futures.add(future);
            }
            CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
//        Thread.sleep(1000);
            log.debug("主任务执行完毕,最后结果：" + result);
            log.debug("--------------------------");
        }

    }

    private static int sum(int data, List<Integer> result) {
        log.debug("任务：" + data);
        result.add(init + data);
        return init + data;
    }
}
